<?php

class dzf_lib_tdbank extends dzf_base_model {
	public $logPath = '/data/release/mp.discuz.qq.com/logs';
	// public $logPath = '/tmp';

	public function __construct() {
		parent::__construct();
	}

	public function userLog($userData) {
		if($userData) {
			$logData = array(
					'logType' => 2,
					'uin' => is_numeric(self::formatString(DZF::app()->mobile['uin'])) ? DZF::app()->mobile['uin'] : 0,
					'nickname' => self::formatString($userData['nickname']),
					'imei' => self::formatString($userData['mobileid']),
					'reg_date' => gmdate('Ymd', $this->getTime()),
					'reg_time' => time(),
					'ext_int1' => $this->getOS(),
					'ext_int2' => gmdate('Ymd', $this->getTime()),
					'ext_int3' => 0,
					'ext_string1' => '',
					'ext_string2' => '',
					'ext_string3' => '',
                );

			return $this->writeLog(2, $logData);
		}
	}

	/**
	 * 写入用户行为日志
	 * @param unknown $data
	 */
	public function userBehavior($data) {

        $logData = array(
				'logType' => 3,
				'imei' => self::formatString(DZF::app()->mobile['id']),
				'uin' => DZF::app()->mobile['uin'] ? DZF::app()->mobile['uin'] : 0,
				'page_type' => isset($data['page_type']) ? $data['page_type'] : -1,
				'tab_id' => isset($data['tab_id']) ? $data['tab_id'] : -1,
				'os' => $this->getOS(),
				'action' => isset($data['action']) ? $data['action'] : -1,
				'tid' => isset($data['tid']) ? $data['tid'] : -1,
				'date' => gmdate('Ymd', $this->getTime()),
				'time' => time(),  // 这个这个时间必须报标准时间戳，否则有时差
				'fid' => isset($data['fid']) ? $data['fid'] : -1,
				'groupname' => self::formatString($data['groupname']),
				'result' => substr(self::formatString($data['result']), 0, 20000),
				'lbsinfo' => self::formatString(DZF::app()->member['lbsinfo']),
				'algorithm1' => $data['algorithm1'] ? $data['algorithm1'] : 0,
				'algorithm2' => $data['algorithm2'] ? $data['algorithm2'] : 0,
				'algorithm3' => $data['algorithm3'] ? $data['algorithm3'] : 0,
				'ext_int1' => 0,
				'ext_int2' => gmdate('Ymd', $this->getTime()),
				'ext_int3' => 0,
				'ext_string' => '',
				'ext_string' => '',
				'ext_string' => '',
			);

        // 平台和动作字段不正确的记录，抛弃
        if (!intval($logData['os']) || !intval($logData['action'])) {
            return false;
        }

        if (!$logData['lbsinfo']) {
            $logData['lbsinfo'] = NULL;
        }

        // 默认值为 -1
        if (!intval($logData['page_type'])) {
            $logData['page_type'] = -1;
        }

        // 默认值为 -1
        if (!intval($logData['tab_id'])) {
            $logData['tab_id'] = -1;
        }

        // 默认值为 -1
        if (!intval($logData['tid'])) {
            $logData['tid'] = -1;
        }

        // 默认值为 -1
        if (!intval($logData['fid'])) {
            $logData['fid'] = -1;
        }

        if (!$logData['groupname']) {
            $logData['groupname'] = NULL;
        }

		return $this->writeLog(3, $logData);
	}

	public function getFileName($logType) {
		if(!$logType) {
			return false;
		}
		$this->checkLogDirExists();
		$FileName = $this->logPath.'/'.$logType.'_'.gmdate('Ymd_H', $this->getTime()).'.log';
		return $FileName;
	}

	private function getTime() {
		return time() + 8*3600;
	}

	public function writeLog($logType, $logData) {

		if(DZF::ext('dc')->filter_user()) {
			return false;
		}

        // 如果 uin 与 imei 一致，证明uin不存在
        if ($logData['uin'] == $logData['imei']) {
            $logData['uin'] = 0;
        }

        $logStr = implode('|', $logData);

		return file_put_contents($this->getFileName($logType), $logStr."\n", FILE_APPEND);
	}

	public function clearLinefeed($content) {
		$content = preg_replace('/[\r\n|\n|\r]+/is', '[br]', $content);
		return $content;
	}

	private function checkLogDirExists() {
		$logDir = $this->logPath;
		if(!is_dir($logDir)) {
			$this->makeDir($logDir);
		}
	}

	private function makeDir($dir, $index = true) {
		$res = true;
		if(!is_dir($dir)) {
			$res = @mkdir($dir, 0777);
			$index && @touch($dir.'/index.html');
		}
		return $res;
	}

    // 格式化字符串，空字符串，特殊字符
    public static function formatString($str) {

        if (!$str) {
            return '';
        }
        if ($str == 'NULL' || $str == 'null') {
            return '';
        }
        $str = str_replace('|', '@', $str);

        return $str;
    }


    /**
     * 获取OS类型
     * 1：and
     * 2：ios
     * @return int
     */
    public function getOS() {

        if (!DZF::app()->mobiletype[DZF::app()->mobile['pf']]) {
            return -1;
        }

        return DZF::app()->mobiletype[DZF::app()->mobile['pf']];
    }
}

